﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTSDAL.LINQSQL.Model;

namespace UTSDAL.LINQSQL.DAO
{
    public class ModuleEventDAO:GenericDAO<ModuleEvent,Guid>
    {
        

        public List<ModuleEvent> findModuleEventByModuleId(Guid moduleId)
        {
            var moduleEventsVar = from m in base.Context.ModuleEvents
                                  where m.Module.ModuleId.Equals(moduleId)
                                  select m;

            return moduleEventsVar.ToList<ModuleEvent>();
        }

        public List<ModuleEvent> findModuleEventByModule(Module module)
        {
            var moduleEventsVar = from m in base.Context.ModuleEvents
                                  where m.Module.Equals(module)                                  
                                  select m;

            return moduleEventsVar.Distinct<ModuleEvent>().ToList<ModuleEvent>();
        }


        /// <summary>
        /// looks for all module events that have already been allocated
        /// </summary>
        /// <param name="semester"></param>
        /// <param name="module"></param>
        /// <returns></returns>
        public List<ModuleEvent> getAllocatedModuleEventBySemesterAndModule(Semester semester, Module module)
        {
            var query = from timetableEvent in base.Context.TimetableEvents
                        where
                        (timetableEvent.Module.Equals(module))
                        &&
                        (timetableEvent.Semester.Equals(semester))
                        select timetableEvent.ModuleEvent;


            return query.ToList<ModuleEvent>();

        }

        /// <summary>
        /// looks for all module events that have not been allocated to a module
        /// during a semester. This is mainly done prior to timetable generation
        /// such that reservations are regenerated.
        /// </summary>
        /// <param name="semester">semester during which the module has not been allocated</param>
        /// <param name="module">module for which module events are being looked up for</param>
        /// <returns></returns>
        public List<ModuleEvent> getUnAllocatedModuleEventBySemesterAndModule(Semester semester, Module module)
        {
            List<ModuleEvent> unAllocatedModuleEvents = new List<ModuleEvent>();
            List<ModuleEvent> allocatedModuleEvents = getAllocatedModuleEventBySemesterAndModule(semester, module);

            foreach (ModuleEvent moduleEvent in module.ModuleEvents)
            {
                if (!allocatedModuleEvents.Contains(moduleEvent))
                {
                    unAllocatedModuleEvents.Add(moduleEvent);
                }

            }

            return unAllocatedModuleEvents;
        }

        
    }
}
